package plots;

import java.util.Comparator;

import agents.AverageUtility;


/**
 * A harvestable plot.
 */
public class Plot implements Comparable<Plot> {
	
	// used for sorting plots in decreasing order based on their average utilities
	protected static class PlotUtilityComparator implements Comparator<Plot> {
		@Override
		public int compare(Plot plot1, Plot plot2) {
			return (int) Math.signum(plot2.getAverageUtility() - plot1.getAverageUtility());
		}
	}
	
	
	public static final PlotUtilityComparator PLOT_UTILITY_COMPARATOR =
			new PlotUtilityComparator();
	
	private static int nextId = 0;
	
	protected int id;
	protected double yield;
	protected double maxYield;
	
	//	The utility that this plot has provided to all those that visited it
	public AverageUtility averageUtility;
	
	/**
	 * Allocates a new plot, with a unique identifier.
	 * 
	 * @param maxYield the maximum yield of the plot
	 */
	public Plot(double maxYield) {
		this(nextId++, maxYield);
	}
	
	/**
	 * Allocates a new plot with the given identifier.
	 * 
	 * @param id the identifier
	 * @param maxYield the maximum yield of the plot
	 */
	public Plot(int id, double maxYield) {
		this.id = id;
		this.maxYield = maxYield;
		this.yield = maxYield;
		this.averageUtility = new AverageUtility();
	}	
	
	public double getYield() {
		return yield;
	}
	
	public double getMaxYield() {
		return maxYield;
	}
	
	public void setYield(double yield) {
		if (yield >=0 )
			this.yield = yield;
		else 
			yield = 0;
	}
	
	public void resetPlotHistory() {
		this.averageUtility.clear();
	}
	
	
	@Override
	public int compareTo(Plot other) {
		return id - other.id;
	}
	
	@Override
	public boolean equals(Object other) {
		if (!(other instanceof Plot))
			return false;
		
		return id == ((Plot) other).id;
	}
	
	@Override
	public int hashCode() {
		return id;
	}
	
	public double getAverageUtility() {
		return averageUtility.getAverageUtility();
	}
}
